home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / atrun.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  2KB  |  54 lines

  1. /* atrun - perform the work 'at' has squirreled away    Author: Jan Looyen */
  2.  
  3. /*-------------------------------------------------------------------------*
  4.  *    atrun scans directory /usr/spool/at for 'at' jobs to be executed.  *
  5.  *    Finished jobs have been moved to directory /usr/spool/at/past.     *
  6.  *-------------------------------------------------------------------------*/
  7. #include <sys/types.h>
  8. #include <sys/dir.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <limits.h>
  12. #include <time.h>
  13. #include <stdio.h>
  14.  
  15. main()
  16. {
  17.   int fd, nr;
  18.   char realtime[15], procname[35], procpast[35];
  19.   struct direct dirbuf;
  20.   struct tm *p, *localtime();
  21.   struct stat sbuf;
  22.   time_t clock;
  23.  
  24. /*-------------------------------------------------------------------------*
  25.  *    Compute real time,  move 'at' jobs whose filenames < real time to  *
  26.  *    /usr/spool/at/past and start a sh for each job.               *
  27.  *-------------------------------------------------------------------------*/
  28.   time(&clock);
  29.   p = localtime(&clock);
  30.   sprintf(realtime, "%02d.%03d.%02d%02d.00",
  31.     p->tm_year % 100, p->tm_yday, p->tm_hour, p->tm_min);
  32.   if ((fd = open("/usr/spool/at", O_RDONLY)) > 0)
  33.     while (read(fd, (char *) &dirbuf, sizeof(dirbuf)) > 0)
  34.         if (dirbuf.d_ino > 0 &&
  35.             dirbuf.d_name[0] != '.' &&
  36.             dirbuf.d_name[0] != 'p' &&
  37.             strncmp(dirbuf.d_name, realtime, 11) <= 0) {
  38.  
  39.             sprintf(procname, "/usr/spool/at/%.14s", dirbuf.d_name);
  40.             sprintf(procpast, "/usr/spool/at/past/%.14s", dirbuf.d_name);
  41.  
  42.             if (fork() == 0)    /* code for child */
  43.                 if (link(procname, procpast) == 0) {    /* link ok? */
  44.                     unlink(procname);
  45.                     stat(procpast, &sbuf);
  46.                     setgid(sbuf.st_uid);
  47.                     setuid(sbuf.st_gid);
  48.                     execl("/bin/sh", "sh", procpast, (char *) 0);
  49.                     fprintf(stderr, "proc %s can't start\n", procpast);
  50.                     exit(1);
  51.                 }
  52.         }
  53. }
  54.